package com.lvfeng.controller;

import com.baidu.aip.face.AipFace;
import com.baidu.aip.imageclassify.AipImageClassify;
import com.baidu.aip.ocr.AipOcr;
import com.baidu.aip.speech.AipSpeech;
import com.lvfeng.conf.API;
import com.lvfeng.tool.EncryptUtil;
import com.lvfeng.tool.JSONChange;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * 百度人工智能程序接口控制器
 */
@RestController("/ai")
public class AIController {

    /**
     * A.文字识别功能
     * @param file
     * @return
     * @throws Exception
     */
    //001.通用文字识别
    @ResponseBody
    @CrossOrigin    //接收跨域请求
    @RequestMapping(value = "/ocr",method = RequestMethod.POST)
    public Map ocr(MultipartFile file) throws Exception{
        //接收图像二进制数据
        byte[] buf = file.getBytes();
        //参数解密
        String key = API.ENCRYPT_KEY;
        EncryptUtil des = new EncryptUtil(key, "utf-8");
        //初始化百度接口
        AipOcr client = new AipOcr(des.deCode(API.APP_ID_TEXT),des.deCode(API.API_KEY_TEXT),des.deCode(API.SECRET_KEY_TEXT));
        //首选参数
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("language_type", "CHN_ENG");    //中英语言
        JSONObject res = client.basicGeneral(buf, options);
        Map map = JSONChange.json2map(res.toString());
        System.out.println(map.toString());
        return map;
    }

    //002.身份证识别
    @ResponseBody
    @CrossOrigin
    @RequestMapping(value = "/idCard",method = RequestMethod.POST)
    public Map idCard(@RequestParam("file_idcard") MultipartFile[] fileArr) throws Exception{
        String key = API.ENCRYPT_KEY;
        EncryptUtil des = new EncryptUtil(key, "utf-8");
        MultipartFile mf1 = fileArr[0];
        MultipartFile mf2 = fileArr[1];
        byte[] by1 = mf1.getBytes();
        byte[] by2 = mf2.getBytes();
        AipOcr client1 = new AipOcr(des.deCode(API.APP_ID_TEXT),des.deCode(API.API_KEY_TEXT),des.deCode(API.SECRET_KEY_TEXT));
        JSONObject jo_fr = client1.idcard(by1,"front",new HashMap<String, String>());    //正面
        System.out.println("正面：" + jo_fr);
        AipOcr client2 = new AipOcr(des.deCode(API.APP_ID_TEXT),des.deCode(API.API_KEY_TEXT),des.deCode(API.SECRET_KEY_TEXT));
        JSONObject jo_ba = client2.idcard(by2,"back",new HashMap<String, String>());     //背面
        System.out.println("背面：" + jo_ba);
        Map map = new HashMap<String,String>();
        map.put("front",JSONChange.json2map(jo_fr.toString()));
        map.put("back",JSONChange.json2map(jo_ba.toString()));
        System.out.println(map.toString());
        return map;
    }
    //003.图像主体识别
    @ResponseBody
    @CrossOrigin
    @RequestMapping(value = "/imageRecognition",method = RequestMethod.POST)
    public Map imageRecognition(@RequestParam("file_image") MultipartFile file) throws Exception{
        byte[] buf = file.getBytes();
        String key = API.ENCRYPT_KEY;
        EncryptUtil des = new EncryptUtil(key, "utf-8");
        AipImageClassify client = new AipImageClassify(des.deCode(API.APP_ID_IMAGE),des.deCode(API.API_KEY_IMAGE),des.deCode(API.SECRET_KEY_IMAGE));
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("baike_num","1");   //返回一条百度百科
        JSONObject res = client.advancedGeneral(buf, options);
        Map map = JSONChange.json2map(res.toString());
        System.out.println(map.toString());
        return map;
    }
    //004.语音识别
    @ResponseBody
    @CrossOrigin
    @RequestMapping(value = "/voiceRecognition",method = RequestMethod.POST)
    public Map voiceRecognition(@RequestParam("audioData") MultipartFile[] file) throws Exception{
        MultipartFile mf1 = file[0];
        System.out.println(mf1);
        byte[] buf = file[0].getBytes();
//        byte[] buf = fd.getBytes();
        String key = API.ENCRYPT_KEY;
        EncryptUtil des = new EncryptUtil(key, "utf-8");
        AipSpeech client = new AipSpeech(des.deCode(API.APP_ID_SOUND),des.deCode(API.API_KEY_SOUND),des.deCode(API.SECRET_KEY_SOUND));
        JSONObject res = client.asr(buf, "wav", 16000, null);
        Map map = JSONChange.json2map(res.toString());
        System.out.println(map.toString());
        return map;
    }
    //005.人脸识别登录
    @ResponseBody
    @CrossOrigin
    @RequestMapping(value = "/faceLogin",method = RequestMethod.POST)
    public Map faceLogin(@RequestParam("snapData") String str) throws Exception{
        //使用Ajax提交base64字符串，需要经过去头转码
        String img_data = str.substring(22, str.length());
//        byte[] by = ChangeEncode.base64ToByte(img_data);    //base64转byte
        String key = API.ENCRYPT_KEY;
        EncryptUtil des = new EncryptUtil(key, "utf-8");
        AipFace client = new AipFace(des.deCode(API.APP_ID_FACE), des.deCode(API.API_KEY_FACE), des.deCode(API.SECRET_KEY_FACE));
        HashMap<String,String> options = new HashMap<String,String>();  //请求预置参数
        options.put("liveness_control","NORMAL");   //活体检测，级别低到高：LOW，NORMAL，HIGH
        JSONObject res = client.search(img_data,"BASE64","DT_user_group_1", options);    //人脸库搜索
        Map map = JSONChange.json2map(res.toString());  //json转map
        return map;
    }
    //006.人脸库注册
    @ResponseBody
    @CrossOrigin
    @RequestMapping(value = "/faceReg",method = RequestMethod.POST)
    public Map faceReg(@RequestParam("snapData") String str,@RequestParam("username") String username) throws Exception{
        String img_data = str.substring(22, str.length());
//        byte[] by = ChangeEncode.base64ToByte(img_data);    //base64转byte
        String key = API.ENCRYPT_KEY;
        EncryptUtil des = new EncryptUtil(key, "utf-8");
        AipFace client = new AipFace(des.deCode(API.APP_ID_FACE), des.deCode(API.API_KEY_FACE), des.deCode(API.SECRET_KEY_FACE));
        HashMap<String,String> options = new HashMap<String,String>();  //请求预置参数
        options.put("liveness_control","NORMAL");   //活体检测，级别低到高：LOW，NORMAL，HIGH
        // 用户最大256B
        JSONObject res = client.addUser(img_data,"BASE64","DT_user_group_1","",options);
        Map map = JSONChange.json2map(res.toString());  //json转map
        return map;
    }
}
